package com.qf.gh.dao.impl;

import com.qf.gh.dao.AdminDao;
import com.qf.gh.domain.Goods;
import com.qf.gh.domain.User;
import com.qf.gh.utils.DateSourceUtils;
import com.qf.gh.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    @Override
    public User select(String username, String password) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());

        User admin=null;
        String sql="select id ,username, password ,email, gender, flag ,role, code from tb_user where username=? and password=md5(?) and role=0";
        try {
            admin=qr.query(sql,new BeanHandler<>(User.class),username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public List<User> selectUser(int i) {
        Connection conn = TxUtils.getConn();
        QueryRunner qr=new QueryRunner();
        String sql="select id ,username, password ,email, gender, flag ,role, code from tb_user where  role=1 and flag=?";
        try {
           List<User> userList=qr.query(conn,sql,new BeanListHandler<>(User.class),i);
           return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> selectOne(String where, List<Object> parmas) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select id ,username, password ,email, gender, flag ,role, code from tb_user"+ where+ " and role=1 and flag=1";
        try {
            List<User> user=qr.query(sql,new BeanListHandler<>(User.class),parmas.toArray());
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void delete(int parseInt) {
        Connection conn = TxUtils.getConn();
        QueryRunner qr=new QueryRunner();
        String sql="delete from tb_user where id=?";
        try {
            qr.update(conn,sql,parseInt);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void allow(int parseInt) {
        Connection conn = TxUtils.getConn();
        QueryRunner qr= new QueryRunner();
        String sql = "UPDATE `tb_user` SET `flag`=1 WHERE `id`=? ";
        try {
            qr.update(conn,sql,parseInt);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("通过用户失败失败");
        }
    }

    @Override
    public List<User> selectValidOne(String toString, List<Object> parmas, int i) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select id ,username, password ,email, gender, flag ,role, code from tb_user"+ toString+ " and role=1 and flag=2";
        try {
            List<User> user=qr.query(sql,new BeanListHandler<>(User.class),parmas.toArray());
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long selectUserCount(int i) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select count(0) from tb_user where  role=1 and flag=?";
        try {
            long count= (long) qr.query(sql,new ScalarHandler(),i);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<User> findData(int i, int pageSize1) {
        QueryRunner qr = new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "select id ,username, password ,email, gender, flag ,role, code from tb_user where flag=1 and role=1  limit ?, ?";
        try {
            List<User> list = qr.query(sql,new BeanListHandler<>(User.class),i,pageSize1);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("获取用户每页数据失败");
        }
        return null;
    }
}
